<template>
  <div>
    <BasicTable @rowClick="rowClick" :handle-search-info-fn="handleSearchInfoFn"
                @register="registerTable" :rowSelection="rowSelection" :formActions="formActions">
      <template #action="{record}">
        <a-popconfirm title="确定删除吗" @confirm="() => remove(record)">
          <a-button type="link" @click.stop="() => {}">删除</a-button>
        </a-popconfirm>
      </template>
    </BasicTable>
    <LogModal ref="refLogModal" @ok="reload" />
  </div>
</template>
<script setup>
import BasicTable from "@/components/Table/src/BasicTable.vue";
import { defHttp } from "@/utils/http/axios";
import { useListPage } from "@/hooks/system/useListPage";
import { real } from "@/utils";
import { DeleteOutlined } from "@ant-design/icons-vue";
import { h, shallowRef } from "vue";
import LogModal from "@/views/cwqs/LogModal.vue";

const refLogModal = shallowRef();
defineOptions({ name: "Logs" });
const Api = {
  getAllLogs: "/cwqs/cwqsLog/list"
};
const columns = [
  {
    dataIndex: "jobName",
    title: "任务名称"
  },
  {
    dataIndex: "createTime",
    title: "采集时间"
  }
];
const getAllLogs = (params) => defHttp.get({
  url: Api.getAllLogs,
  params
}, { joinParamsToUrl: true });
const { tableContext } = useListPage({
  designScope: "cwqs-data-log-list",
  tableProps: {
    title: "财务取数日志",
    api: getAllLogs,
    columns,
    formConfig: {
      schemas: [
        {
          label: "任务名称",
          field: "jobName",
          component: "Input",
          colProps: { span: 6 }
        },
        {
          label: "起始日期",
          field: "createTime_begin",
          component: "DatePicker",
          componentProps: {
            style: {
              width: "100%"
            }
          }
        },
        {
          label: "截止日期",
          field: "createTime_end",
          component: "DatePicker",
          componentProps: {
            style: {
              width: "100%"
            }
          }
        }
      ]
    },
    rowSelection: {
      getCheckboxProps() {
        return {
          onClick(e) {
            e.stopPropagation();
          }
        };
      }
    }
  }
});
const formActions = () => [
  {
    title: "批量删除",
    icon: h(DeleteOutlined),
    danger: true,
    disabled: real(selectedRowKeys).length <= 0,
    confirmTitle: "确定删除吗",
    confirmInfo: "删除后无法恢复, 确定删除吗",
    confirm() {
      defHttp.delete({
        url: "/cwqs/cwqsLog/deleteBatch",
        params: {
          ids: real(selectedRowKeys).toString()
        }
      }, { joinParamsToUrl: true }).then(reload);
    }
  }
];

function remove(record) {
  record = real(record);
  defHttp.delete({
    url: "/cwqs/cwqsLog/delete",
    params: { id: record.id }
  }, { joinParamsToUrl: true }).then(reload);
}

const [registerTable, { reload, updateTableDataRecord }, {
  rowSelection,
  selectedRows,
  selectedRowKeys
}] = tableContext;

function handleSearchInfoFn(info) {
  if (info.jobName) info.jobName = `*${info.jobName}*`;
}

function rowClick(record) {
  record = real(record);
  refLogModal.value.setData({id: record.id})
}
</script>
